﻿import QtQuick 2.0

import "./components"
import "./pageComponents"
import "./animation"

Item {
    id: root
    height: parent.height; width: parent.width


    property int animShowMode: 0   // 0正常 1向左扩大
    property int animFullScreen: 0 // 0不全屏 1全屏



    property var logInfo: [{
            "FinishTime": "0",
            "Time": "0",
            "Step": "0",
            "Status": "进行中",
            "ActionInfo": "1"
        }]


    property bool mutexExpInfo: false
    onVisibleChanged: {
        if(visible){
            state2Edit()
        }else{
            gStateExp.isConfirm_3 = false;
        }
    }

    function state2Edit(){
        combo_mainMethod.setText(gStateExp.mainMethod)
        combo_singleMethod.setText(gStateExp.singleMethod_3)
        updateExpModel()
        updateLogInfo()
    }

    // 更新当前页面的实验日志
    function updateLogInfo(){
        var logInfo
        if(gExpIsRunning === 1){       // 运行中/暂停
            logInfo = CGetMessageInterface.getExpLog(3)
        }else if(gExpIsRunning === 0){     // 空闲/复位
            let module = gStateExp.mainMethod === "" ? 1 : 0
            let name = gStateExp.mainMethod === "" ? gStateExp.singleMethod_3 : gStateExp.mainMethod

            // 主流程
            if(gStateExp.mainMethod !== ""){
                CGetMessageInterface.refMainExpLog(gStateExp.mainMethod)    // 通知后端刷新日志
            }
            logInfo = CGetMessageInterface.getExperimentLog(3, gStateExp.mainMethod === ""?3:0, gStateExp.singleMethod_3)
        }
        expLog.setData(logInfo)


        // 步骤数
        if(gStateExp.mainMethod === ""){
            gStateExp.expStepCount = CGetMessageInterface.getExperimentStepCount(3)
        }else{
            gStateExp.expStepCount = CGetMessageInterface.getExperimentStepCount(0)
        }
    }


    // 更新实验名称的model
    function updateExpModel(){

        mutexExpInfo = true

        var currentModel = combo_mainMethod.getText()
        combo_mainMethod.model = CGetMessageInterface.getMainMethodList()
        combo_mainMethod.setIndex(combo_mainMethod.model.indexOf(currentModel))

        currentModel = combo_singleMethod.getText()
        combo_singleMethod.model = CGetMessageInterface.getSingleMethodList(3);
        if(combo_mainMethod.getText() === ""){
            combo_singleMethod.setIndex(combo_singleMethod.model.indexOf(currentModel))
        }else{
            combo_singleMethod.setText(CGetMessageInterface.getSingleMethodByMainMethod(combo_mainMethod.getText())[2])
        }

        mutexExpInfo = false

    }


    ControlPannel{
        id: controlPannel
    }
    // 左边的动画
    AnimationLayout3{
        id: layout_animation
        radius: 3*dp
        y: {
            if(animFullScreen === 1)     return 0
            else                         return controlPannel.y + controlPannel.height
        }
        height: root.height - y
        width: {
            if(animShowMode === 0 && animFullScreen === 0)  return root.width - layout_info.width - 10*dp
            else                              return root.width
        }
        onFullScreen: {
            animFullScreen = 1 - animFullScreen
        }
        CustomImage{
            visible: animFullScreen !== 1     // 全屏时不显示
            width: 12*dp;  height: 82*dp
            anchors.verticalCenter: parent.verticalCenter
            source: animShowMode===0 ? "qrc:/picture/animation/leftExpand.png" : "qrc:/picture/animation/leftShrink.png"
            x: parent.width - width
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape: "PointingHandCursor"
                onEntered: { parent.opacity = 0.8 }
                onExited: { parent.opacity = 1 }
                onClicked: {
                    animShowMode = 1 - animShowMode
                }
            }
        }
        z: 100
    }
    // 右边的实验信息
    Item{
        id: layout_info
        x: layout_animation.x + layout_animation.width + 10*dp
        y: controlPannel.y + controlPannel.height
        height: root.height - y;  width: 610*dp

        // 实验信息
        Rectangle{
            id: layout_expInfo
            height: column_editArea.implicitHeight + 80*dp; width: parent.width
            radius: 5*dp
            Item{
                height: 30*dp; width: parent.width - 2*x
                x: 22*dp; y: 20*dp
                Text{
                    text: "实验信息"
                    font.bold: true
                    font.pixelSize: config.fontsize_1
                }
            }

            Column{
                id: column_editArea
                x: 22*dp; y: 60*dp
                height: 40; width: parent.width
                spacing: 15*dp
                CustomComboBox{
                    id: combo_mainMethod
                    title: "项目名称"

                    titleWidth: 115*dp
                    editWidth: 440*dp
                    enabled: gExpIsRunning === 0
                    showClearButton: true
                    onClickText: {
                        gStateExp.mainMethod = text
                        if(index===-1){         // 清空
                            gStateExp.clearExpSelected()
                        }
                        state2Edit()
                    }
                }CustomComboBox{
                    id: combo_singleMethod
                    enabled: combo_mainMethod.text === "" && gExpIsRunning === 0
                    title: "模块实验名称"

                    titleWidth: 115*dp
                    editWidth: 440*dp
                    onClickText: {
                        gStateExp.singleMethod_3 = text
                        state2Edit()
                    }
                }
            }

        }
        // 实验日志
        Rectangle{
            id: layout_expLog
            y: layout_expInfo.y + layout_expInfo.height + 10*dp
            height: parent.height - y; width: parent.width
            radius: 5*dp
            ExperimentLog{
                id: expLog
            }
        }

    }

}
